﻿using AutoMapper;
using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using XsqTech.SmartOA.Domain.Table;
using XsqTech.SmartOA.Dtos.DTO;
using XsqTech.SmartOA.Dtos.XSPDto.FromDto;
using XsqTech.SmartOA.EFCore.Respository;
using XsqTech.SmartOA.IRespository;
using XsqTech.SmartOA.IService;
using XsqTech.SmartOA.Service.DTO;

namespace XsqTech.SmartOA.Service
{
    public class FormService : IFormService
    {
        IFormRespository _baseForm;
        readonly IMapper _mapper;
        readonly IUnitOfWork _unitOfWork;
        private readonly ILogger<FormService> _logger;
        //构造函数
        public FormService(IFormRespository baseForm, IMapper mapper, IUnitOfWork unitOfWork, ILogger<FormService> logger)
        {
            _baseForm = baseForm;
            _mapper = mapper;
            _unitOfWork = unitOfWork;
            _logger = logger;
        }
        /// <summary>
        /// 表单添加
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        public ResponseModel AddFrom(AddFromDto dto)
        {
            try
            {
                //判断表单名称是否重复
                var list = _baseForm.Query().Where(x => x.FormName == dto.FormName);
                if (list.Count() > 0)
                {
                    return new ResponseModel
                    {
                        Code = State.请求失败,
                        Message = "表单名称已从在",
                    };
                }
                var s = _mapper.Map<Form>(dto);
                _baseForm.Add(s);
                int data = _unitOfWork.SaveChangs();
                if (data > 0)
                {
                    return new ResponseModel
                    {
                        Code = State.请求成功,
                        Message = "添加成功",
                        Count = data,
                    };
                }
                else
                {
                    return new ResponseModel
                    {
                        Code = State.请求失败,
                        Message = "添加失败",
                        Count = data,
                    };
                }

            }
            catch (Exception ex)
            {
                //控制台输出
                Console.WriteLine(ex.Message);
                //写入日志
                _logger.LogError(ex.Message);
                throw;
            };
        }
        /// <summary>
        /// 表单显示
        /// </summary>
        /// <param name="UserName"></param>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <returns></returns>
        public ResponseModel FormQuery(string Name, int pageIndex, int pageSize)
        {
            var list = _baseForm.Query();
            if (!string.IsNullOrEmpty(Name))
            {
                list = list.Where(x => x.FormName.Contains(Name));
            }

            int Count = list.Count();
            var Plist = list.Skip((pageIndex - 1) * pageSize).Take(pageSize).ToList();

            return new ResponseModel
            {
                Code = State.请求成功,
                Message = "查询成功",
                Data = Plist,
                Count = Count,
            };
        }
        /// <summary>
        /// 表单删除（单删）
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public ResponseModel FormDelete(string id)
        {
            try
            {
                var list = _baseForm.Query().FirstOrDefault(x => x.Id.ToString().Equals(id));
                _baseForm.Delete(list.Id);
                int data = _unitOfWork.SaveChangs();
                if (data > 0)
                {
                    return new ResponseModel
                    {
                        Code = State.请求成功,
                        Message = "删除成功",
                        Count = data,
                    };
                }
                else
                {
                    return new ResponseModel
                    {
                        Code = State.请求失败,
                        Message = "删除失败",
                        Count = data,
                    };
                }
            }
            catch (Exception ex)
            {
                //控制台输出
                Console.WriteLine(ex.Message);
                //写入日志
                _logger.LogError(ex.Message);
                throw;
            };
        }
        /// <summary>
        /// 表单删除（批删）
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        public ResponseModel AllFormDelete(List<AddFromDto> dto)
        {
            try
            {
                foreach (var item in dto)
                {
                    var list = _baseForm.Query().FirstOrDefault(x => x.Id == item.Id);
                    if (list != null)
                    {
                        _baseForm.Delete(list.Id);
                    }
                    else
                    {
                        return new ResponseModel
                        {
                            Code = State.请求失败,
                            Message = "删除失败",
                        };
                    }
                }
                int data = _unitOfWork.SaveChangs();
                if (data > 0)
                {
                    return new ResponseModel
                    {
                        Code = State.请求成功,
                        Message = "删除成功",
                        Count = data,
                    };
                }
                else
                {
                    return new ResponseModel
                    {
                        Code = State.请求失败,
                        Message = "删除失败",
                        Count = data,
                    };
                }
            }
            catch (Exception ex)
            {
                //控制台输出
                Console.WriteLine(ex.Message);
                //写入日志
                _logger.LogError(ex.Message);
                throw;
            };

        }
        /// <summary>
        /// 表单反填
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public ResponseModel FormQueryId(string id)
        {
            var list = _baseForm.Query().FirstOrDefault(x=>x.Id.ToString().Equals(id));
            return new ResponseModel
            {
                Code = State.请求成功,
                Message = "请求成功",
                Data = list,
            };
        }
        /// <summary>
        /// 表单修改
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        public ResponseModel UpdateFrom(AddFromDto dto)
        {
            try
            {
                //判断表单名称是否重复
                //var list = _baseForm.Query().Where(x => x.FormName == dto.FormName);
                //if (list.Count() > 0)
                //{
                //    return new ResponseModel
                //    {
                //        Code = State.请求失败,
                //        Message = "表单名称已从在",
                //    };
                //}
                var s = _mapper.Map<Form>(dto);
                _baseForm.Update(s);
                int data = _unitOfWork.SaveChangs();
                if (data > 0)
                {
                    return new ResponseModel
                    {
                        Code = State.请求成功,
                        Message = "修改成功",
                        Count = data,
                    };
                }
                else
                {
                    return new ResponseModel
                    {
                        Code = State.请求失败,
                        Message = "修改失败",
                        Count = data,
                    };
                }

            }
            catch (Exception ex)
            {
                //控制台输出
                Console.WriteLine(ex.Message);
                //写入日志
                _logger.LogError(ex.Message);
                throw;
            };
        }

    }
}
